package com.lmf.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lmf.eduservice.entity.EduCourse;
import com.lmf.eduservice.entity.EduCourseDescription;
import com.lmf.eduservice.entity.frontVo.CourseFrontVo;
import com.lmf.eduservice.entity.vo.CourseInfoForm;
import com.lmf.eduservice.entity.vo.CoursePublishVo;
import com.lmf.eduservice.mapper.EduCourseMapper;
import com.lmf.eduservice.service.EduChapterService;
import com.lmf.eduservice.service.EduCourseDescriptionService;
import com.lmf.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lmf.eduservice.service.EduVideoService;
import com.lmf.handler.Exception.LmfException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author lmf
 * @since 2022-01-25
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    //课程描述service类注入
    @Autowired
    private EduCourseDescriptionService eduCourseDescriptionService;
    //课程小节service类注入
    @Autowired
    private EduVideoService eduVideoService;
    //课程章节service类注入
    @Autowired
    private EduChapterService eduChapterService;


    //保存添加的课程相关信息
//    @SneakyThrows
    @Override
    public String saveCourseInfo(CourseInfoForm courseInfoForm) throws LmfException {
        //将前端传过来的信息，封装到课程信息表对应的实体类中去，再调用方法保存到数据库
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoForm,eduCourse);
        boolean save = this.save(eduCourse);
        if (!save) {
            throw new LmfException(20001,"添加课程相关信息失败");
        }
        //将前端传过来的课程简介信息，封装到课程简介表对应的实体类中去，再调用方法保存到数据库
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setId(eduCourse.getId());
        eduCourseDescription.setDescription(courseInfoForm.getDescription());
        boolean save1 = eduCourseDescriptionService.save(eduCourseDescription);
        if (!save1) {
            throw new LmfException(20002,"添加课程简介失败");
        }
        return eduCourse.getId();
    }

    //根据课程id，获取相应课程信息
    @Override
    public CourseInfoForm getCourseInfo(String courseId) {
        //1.先查课程表，并封装到响应封装类里面
        EduCourse eduCourse = this.getById(courseId);
        CourseInfoForm courseInfoForm = new CourseInfoForm();
        BeanUtils.copyProperties(eduCourse,courseInfoForm);

        //2.再查课程简介表，并封装到响应封装类里面
        EduCourseDescription eduCourseDescription = eduCourseDescriptionService.getById(courseId);
        courseInfoForm.setDescription(eduCourseDescription.getDescription());

        return courseInfoForm;
    }

    //根据课程id，修改相应课程信息
    @Override
    public void updateCourseInfo(CourseInfoForm courseInfoForm) throws LmfException {
        //1.先修改课程表
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoForm,eduCourse);
        boolean b = this.updateById(eduCourse);
        if (!b) {
            throw new LmfException(20001,"修改课程表信息失败！");
        }
        //2.再修改课程简介表
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setDescription(courseInfoForm.getDescription());
        eduCourseDescription.setId(courseInfoForm.getId());
        boolean b1 = eduCourseDescriptionService.updateById(eduCourseDescription);
        if (!b1) {
            throw new LmfException(20002,"修改课程简介表信息失败！");
        }
    }

    @Override
    public CoursePublishVo getPublishCourseInfo(String courseId) {
        return baseMapper.getPublishCourseInfo(courseId);
    }

    @Override
    public boolean deleteCourseById(String courseId) throws LmfException {

        //1.根据课程id，删除课程里面的小节
        eduVideoService.removeVideoByCourseId(courseId);
        //2.根据课程id，删除课程里面的章节
        eduChapterService.removeChapterByCourseId(courseId);
        //3.根据课程id，删除课程描述
        eduCourseDescriptionService.removeById(courseId);
        //4.根据课程id，删除课程
        boolean b = this.removeById(courseId);
        if (b) {
            return true;
        }else {
            throw new LmfException(20001,"删除课程失败！");
        }
    }

    @Override
    public Map<String, Object> getCourseFrontInfo(Page<EduCourse> page, CourseFrontVo courseFrontVo) {
        String title = null;
        String subjectId = null;
        String subjectParentId = null;
        String gmtCreateSort = null;
        String buyCountSort = null;
        String priceSort = null;
        String teacherId = null;

        if (!StringUtils.isEmpty(courseFrontVo)){
            title = courseFrontVo.getTitle();
            subjectId = courseFrontVo.getSubjectId();
            subjectParentId = courseFrontVo.getSubjectParentId();
            gmtCreateSort = courseFrontVo.getGmtCreateSort();
            buyCountSort = courseFrontVo.getBuyCountSort();
            priceSort = courseFrontVo.getPriceSort();
            teacherId = courseFrontVo.getTeacherId();
        }


        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        //判断条件值是否为空，不为空拼接条件
        if (!StringUtils.isEmpty(subjectParentId)){//一级分类
            wrapper.eq("subject_parent_id",subjectParentId);
        }
        if (!StringUtils.isEmpty(subjectId)){//二级分类
            wrapper.eq("subject_id",subjectId);
        }
        if (!StringUtils.isEmpty(buyCountSort)){//关注度
            wrapper.orderByDesc("buy_count");
        }
        if (!StringUtils.isEmpty(priceSort)){//价格
            wrapper.orderByDesc("price");
        }
        if (!StringUtils.isEmpty(gmtCreateSort)){//最新，创建时间
            wrapper.orderByDesc("gmt_create");
        }

        //进行分页查询
        baseMapper.selectPage(page, wrapper);

        long total = page.getTotal();//总记录数
        List<EduCourse> courseList = page.getRecords();//数据集合
        long size = page.getSize();//每页记录数
        long current = page.getCurrent();//当前页
        long pages = page.getPages();//总页数
        boolean hasPrevious = page.hasPrevious();//是否有上一页
        boolean hasNext = page.hasNext();//是否有下一页

        HashMap<String, Object> map = new HashMap<>();
        map.put("total",total);
        map.put("list",courseList);
        map.put("size",size);
        map.put("current",current);
        map.put("pages",pages);
        map.put("hasPrevious",hasPrevious);
        map.put("hasNext",hasNext);

        return map;

    }
}
